using System;
using System.Collections;
using I2.Loc;
using UnityEngine;

public class BCWFlag : MonoBehaviour, IPunObservable
{
	[SerializeField]
	public float radius;

	[SerializeField]
	private float timeForPoint = 0.5f;

	[SerializeField]
	private float timeForReturnFlag = 1f;

	[SerializeField]
	private int point = 1;

	[SerializeField]
	private Transform startPoint;

	[SerializeField]
	private Transform endPoint;

	[SerializeField]
	private Color myTeamColorFlag;

	[SerializeField]
	private Color enemyTeamColorFlag;

	[SerializeField]
	private Color neutralTeamColorFlag;

	[SerializeField]
	private Renderer flagRenderer;

	[SerializeField]
	private Renderer[] areaRenderer;

	[SerializeField]
	private GameObject flag;

	[SerializeField]
	public CapturedTeam capturedFlag;

	[SerializeField]
	private bool _movingFlag = true;

	[HideInInspector]
	public bool iCapturedFlag;

	[HideInInspector]
	public CaptureMsg captureMessage;

	[HideInInspector]
	public int redTeamScore;

	[HideInInspector]
	public int blueTeamScore;

	private CapturedTeam lastCapturedFlag;

	private float distanceForFlag;

	private PhotonView photonView;

	private Vector2 centerPoint;

	private NJGMapItem iconMinimap;

	private PointerPanel pointFlag;

	public float maxCountForCapture = 100f;

	public string _nameFlag = string.Empty;

	private bool messageCapturedEnemyFlagRT;

	private bool messageCapturedEnemyFlagBT;

	public static event Action CapturedFlag;

	private void Awake()
	{
		if (GameController.instance != null && !GameController.instance.IsFlagGameType())
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		photonView = GetComponent<PhotonView>();
		iconMinimap = base.gameObject.AddComponent<NJGMapItem>();
		iconMinimap.SetType("Flag" + _nameFlag);
		centerPoint.x = base.transform.position.x;
		centerPoint.y = base.transform.position.z;
		if (_movingFlag)
		{
			distanceForFlag = endPoint.position.y - startPoint.position.y;
		}
		StartCoroutine(UpdateFlag());
		StartCoroutine(IEReturnFlag());
	}

	private void Start()
	{
		AddPointOnFlag(string.Empty);
		if (BCWFlagController.instance != null)
		{
			BCWFlagController.instance.AddFlag(this);
		}
	}

	public void UpdateInfoForFlag()
	{
		switch (capturedFlag)
		{
		case CapturedTeam.Blue:
			if (TDMController.myTeam == PunTeams.Team.blue)
			{
				for (int l = 0; l < areaRenderer.Length; l++)
				{
					areaRenderer[l].material.color = myTeamColorFlag;
				}
				AddPointOnFlag("Blue");
			}
			else
			{
				for (int m = 0; m < areaRenderer.Length; m++)
				{
					areaRenderer[m].material.color = enemyTeamColorFlag;
				}
				AddPointOnFlag("Red");
			}
			break;
		case CapturedTeam.Red:
			if (TDMController.myTeam == PunTeams.Team.red)
			{
				for (int j = 0; j < areaRenderer.Length; j++)
				{
					areaRenderer[j].material.color = myTeamColorFlag;
				}
				AddPointOnFlag("Blue");
			}
			else
			{
				for (int k = 0; k < areaRenderer.Length; k++)
				{
					areaRenderer[k].material.color = enemyTeamColorFlag;
				}
				AddPointOnFlag("Red");
			}
			break;
		default:
		{
			for (int i = 0; i < areaRenderer.Length; i++)
			{
				areaRenderer[i].material.color = neutralTeamColorFlag;
			}
			AddPointOnFlag(string.Empty);
			break;
		}
		}
	}

	private void OnDestroy()
	{
		StopCoroutine(UpdateFlag());
		StopCoroutine(IEReturnFlag());
	}

	public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (PVPManager.instance != null && !PVPManager.instance.gameIsStarted)
		{
			return;
		}
		if (stream.isWriting)
		{
			stream.SendNext(redTeamScore);
			stream.SendNext(blueTeamScore);
			stream.SendNext((int)capturedFlag);
		}
		else
		{
			redTeamScore = (int)stream.ReceiveNext();
			blueTeamScore = (int)stream.ReceiveNext();
			int num = (int)stream.ReceiveNext();
			capturedFlag = (CapturedTeam)num;
		}
		if (TDMController.myTeam == PunTeams.Team.blue && (float)blueTeamScore >= maxCountForCapture)
		{
			messageCapturedEnemyFlagBT = true;
		}
		if (TDMController.myTeam == PunTeams.Team.red && (float)redTeamScore >= maxCountForCapture)
		{
			messageCapturedEnemyFlagRT = true;
		}
		if (messageCapturedEnemyFlagRT && (float)redTeamScore <= maxCountForCapture - 5f)
		{
			if (PVPManager.instance != null && PVPManager.instance.gameIsStarted)
			{
				string text = string.Format(ScriptLocalization.Get("Game_Common/src_AttackedPoint"), _nameFlag);
				GameplayNotificationController.instance.AddNotification(text, 1);
				messageCapturedEnemyFlagRT = false;
			}
		}
		else if (messageCapturedEnemyFlagBT && (float)blueTeamScore <= maxCountForCapture - 5f && PVPManager.instance != null && PVPManager.instance.gameIsStarted)
		{
			string text2 = string.Format(ScriptLocalization.Get("Game_Common/src_AttackedPoint"), _nameFlag);
			GameplayNotificationController.instance.AddNotification(text2, 1);
			messageCapturedEnemyFlagBT = false;
		}
	}

	private IEnumerator UpdateFlag()
	{
		while (true)
		{
			yield return new WaitForSeconds(timeForPoint);
			UpdateCaptureFlag();
			MovingFlag();
			ColorFlag();
		}
	}

	private IEnumerator IEReturnFlag()
	{
		while (true)
		{
			yield return new WaitForSeconds(timeForReturnFlag);
			ReturnFlag();
		}
	}

	public void Reset()
	{
		redTeamScore = 0;
		blueTeamScore = 0;
		messageCapturedEnemyFlagRT = false;
		messageCapturedEnemyFlagBT = false;
		lastCapturedFlag = CapturedTeam.None;
		capturedFlag = CapturedTeam.None;
	}

	private CapturedTeam CaptureFlag()
	{
		CapturedTeam capturedTeam = CapturedTeam.None;
		bool flag = false;
		Vector2 a = default(Vector2);
		foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
		{
			if (listPlayer.isDead)
			{
				continue;
			}
			a.x = listPlayer.transform.position.x;
			a.y = listPlayer.transform.position.z;
			if (Vector2.Distance(a, centerPoint) <= radius)
			{
				if (capturedTeam == CapturedTeam.None)
				{
					capturedTeam = ((listPlayer.localTeam != PunTeams.Team.blue) ? CapturedTeam.Red : CapturedTeam.Blue);
				}
				else if ((listPlayer.localTeam == PunTeams.Team.blue && capturedTeam == CapturedTeam.Red) || (listPlayer.localTeam == PunTeams.Team.red && capturedTeam == CapturedTeam.Blue))
				{
					capturedTeam = CapturedTeam.Draw;
				}
			}
		}
		return capturedTeam;
	}

	private void MovingFlag()
	{
		int num = 0;
		if (redTeamScore > 0)
		{
			num = redTeamScore;
		}
		else if (blueTeamScore > 0)
		{
			num = blueTeamScore;
		}
		if (_movingFlag)
		{
			float num2 = distanceForFlag / maxCountForCapture;
			flag.transform.position = startPoint.position + Vector3.up * num2 * num;
		}
	}

	private void UpdateCaptureFlag()
	{
		if (PVPManager.instance != null && PVPManager.instance.gameIsStarted)
		{
			if (PhotonNetwork.isMasterClient && TDMController.instance.CountTeam(PunTeams.Team.blue) > 0 && TDMController.instance.CountTeam(PunTeams.Team.red) > 0)
			{
				switch (CaptureFlag())
				{
				case CapturedTeam.Blue:
					if (redTeamScore <= 0)
					{
						if ((float)blueTeamScore < maxCountForCapture)
						{
							blueTeamScore += point;
						}
					}
					else
					{
						redTeamScore -= point;
					}
					break;
				case CapturedTeam.Red:
					if (blueTeamScore <= 0)
					{
						if ((float)redTeamScore < maxCountForCapture)
						{
							redTeamScore += point;
						}
					}
					else
					{
						blueTeamScore -= point;
					}
					break;
				}
				if ((float)redTeamScore > maxCountForCapture - 1f && capturedFlag != CapturedTeam.Red)
				{
					capturedFlag = CapturedTeam.Red;
					if (BCWFlag.CapturedFlag != null)
					{
						BCWFlag.CapturedFlag();
					}
					BCWFlagController.instance.FlagCaptured();
				}
				else if ((float)blueTeamScore > maxCountForCapture - 1f && capturedFlag != CapturedTeam.Blue)
				{
					capturedFlag = CapturedTeam.Blue;
					if (BCWFlag.CapturedFlag != null)
					{
						BCWFlag.CapturedFlag();
					}
					BCWFlagController.instance.FlagCaptured();
				}
				else if (redTeamScore == 0 && blueTeamScore == 0 && (capturedFlag == CapturedTeam.Blue || capturedFlag == CapturedTeam.Red))
				{
					capturedFlag = CapturedTeam.None;
					if (BCWFlag.CapturedFlag != null)
					{
						BCWFlag.CapturedFlag();
					}
					BCWFlagController.instance.FlagCaptured();
				}
			}
			bool flag = false;
			PlayerBehavior playerBehavior = null;
			int num = 0;
			Vector2 a = default(Vector2);
			foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
			{
				if (listPlayer.isDead)
				{
					continue;
				}
				a.x = listPlayer.transform.position.x;
				a.y = listPlayer.transform.position.z;
				if (Vector2.Distance(a, centerPoint) <= radius)
				{
					if (listPlayer == PlayerBehavior.MyPlayer && ((capturedFlag != (CapturedTeam)((listPlayer.localTeam != PunTeams.Team.blue) ? 1 : 2) && captureMessage == CaptureMsg.Captured) || captureMessage != CaptureMsg.Captured))
					{
						iCapturedFlag = true;
						flag = true;
					}
					if (listPlayer == PlayerBehavior.MyPlayer)
					{
						playerBehavior = listPlayer;
					}
					if (listPlayer.localTeam != TDMController.myTeam)
					{
						num++;
					}
				}
			}
			if (playerBehavior != null)
			{
				if (num > 0)
				{
					captureMessage = CaptureMsg.Draw;
				}
				else if ((capturedFlag == CapturedTeam.Blue && playerBehavior.localTeam == PunTeams.Team.red) || (capturedFlag == CapturedTeam.Red && playerBehavior.localTeam == PunTeams.Team.blue))
				{
					captureMessage = CaptureMsg.Interception;
				}
				else if ((capturedFlag == CapturedTeam.None && redTeamScore > 0 && playerBehavior.localTeam == PunTeams.Team.red) || (blueTeamScore > 0 && playerBehavior.localTeam == PunTeams.Team.blue))
				{
					captureMessage = CaptureMsg.Captured;
				}
				else
				{
					captureMessage = CaptureMsg.None;
				}
			}
			if (!flag)
			{
				iCapturedFlag = false;
			}
			if (lastCapturedFlag != capturedFlag)
			{
				if (BCWFlag.CapturedFlag != null)
				{
					BCWFlag.CapturedFlag();
				}
				ColorArea(capturedFlag, lastCapturedFlag);
				BCWFlagController.instance.FlagCaptured();
				lastCapturedFlag = capturedFlag;
			}
		}
		else
		{
			iCapturedFlag = false;
		}
	}

	private void ColorFlag()
	{
		bool flag = (TDMController.myTeam == PunTeams.Team.red && redTeamScore > 0) || (TDMController.myTeam == PunTeams.Team.blue && blueTeamScore > 0);
		if (redTeamScore <= 0 && blueTeamScore <= 0)
		{
			flagRenderer.material.color = neutralTeamColorFlag;
		}
		else if (flag)
		{
			flagRenderer.material.color = myTeamColorFlag;
		}
		else if (!flag)
		{
			flagRenderer.material.color = enemyTeamColorFlag;
		}
	}

	public void ColorArea(CapturedTeam team, CapturedTeam lastCaptured, bool showMsg = true)
	{
		string text = string.Empty;
		switch (team)
		{
		case CapturedTeam.Blue:
			if (TDMController.myTeam == PunTeams.Team.blue)
			{
				for (int j = 0; j < areaRenderer.Length; j++)
				{
					areaRenderer[j].material.color = myTeamColorFlag;
				}
				text = string.Format(ScriptLocalization.Get("Game_Common/src_CapturedPoint"), _nameFlag, ScriptLocalization.Get("Game_Common/src_MyTeam"));
				AddPointOnFlag("Blue");
			}
			else
			{
				for (int k = 0; k < areaRenderer.Length; k++)
				{
					areaRenderer[k].material.color = enemyTeamColorFlag;
				}
				text = string.Format(ScriptLocalization.Get("Game_Common/src_CapturedPoint"), _nameFlag, ScriptLocalization.Get("Game_Common/src_EnemyTeam"));
				AddPointOnFlag("Red");
			}
			break;
		case CapturedTeam.Red:
			if (TDMController.myTeam == PunTeams.Team.red)
			{
				text = string.Format(ScriptLocalization.Get("Game_Common/src_CapturedPoint"), _nameFlag, ScriptLocalization.Get("Game_Common/src_MyTeam"));
				AddPointOnFlag("Blue");
				for (int l = 0; l < areaRenderer.Length; l++)
				{
					areaRenderer[l].material.color = myTeamColorFlag;
				}
			}
			else
			{
				for (int m = 0; m < areaRenderer.Length; m++)
				{
					areaRenderer[m].material.color = enemyTeamColorFlag;
				}
				text = string.Format(ScriptLocalization.Get("Game_Common/src_CapturedPoint"), _nameFlag, ScriptLocalization.Get("Game_Common/src_EnemyTeam"));
				AddPointOnFlag("Red");
			}
			break;
		default:
		{
			switch (lastCaptured)
			{
			case CapturedTeam.Blue:
				if (TDMController.myTeam == PunTeams.Team.blue)
				{
					text = string.Format(ScriptLocalization.Get("Game_Common/src_PointLost"), _nameFlag);
				}
				break;
			case CapturedTeam.Red:
				if (TDMController.myTeam == PunTeams.Team.red)
				{
					text = string.Format(ScriptLocalization.Get("Game_Common/src_PointLost"), _nameFlag);
				}
				break;
			}
			for (int i = 0; i < areaRenderer.Length; i++)
			{
				areaRenderer[i].material.color = neutralTeamColorFlag;
			}
			AddPointOnFlag(string.Empty);
			break;
		}
		}
		if (showMsg)
		{
			GameplayNotificationController.instance.AddNotification(text, 1);
		}
	}

	private void ReturnFlag()
	{
		if (CaptureFlag() != 0)
		{
			return;
		}
		if (capturedFlag != 0)
		{
			if (capturedFlag == CapturedTeam.Red)
			{
				if ((float)redTeamScore < maxCountForCapture)
				{
					redTeamScore += point;
				}
			}
			else if (capturedFlag == CapturedTeam.Blue && (float)blueTeamScore < maxCountForCapture)
			{
				blueTeamScore += point;
			}
		}
		else
		{
			if (blueTeamScore > 0)
			{
				blueTeamScore -= point;
			}
			if (redTeamScore > 0)
			{
				redTeamScore -= point;
			}
		}
	}

	public void AddPointOnFlag(string capturedTeam)
	{
		PointerController.instance.RemovePointer(pointFlag);
		pointFlag = PointerController.instance.AddPointer(base.transform, 0f, "Flaf_" + _nameFlag + ((!(capturedTeam == string.Empty)) ? "_" : string.Empty) + capturedTeam, true, null, false, false);
		iconMinimap.SetType("Flag" + _nameFlag + ((!(capturedTeam == string.Empty)) ? "_" : string.Empty) + capturedTeam);
	}
}
